package com.sora.cmsservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sora.cmsservice.entity.CrmBanner;
import com.sora.cmsservice.service.CrmBannerService;
import com.sora.commonutils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author sora
 * @since 2023-01-11
 */
@RestController
@RequestMapping("/cmsservice/banneradmin")
@CrossOrigin
public class BannerAdminController {
    @Autowired
    private CrmBannerService bannerService;
    @PostMapping("pageBanner/{page}/{limit}")
    public R pageBanner(@PathVariable Long page,@PathVariable Long limit) {
        Page<CrmBanner> bannerPage = new Page<>(page, limit);
        bannerService.page(bannerPage,null);
        List<CrmBanner> records = bannerPage.getRecords();
        long total = bannerPage.getTotal();
        HashMap<String, Object> map = new HashMap<>();
        map.put("row",records);
        map.put("total",total);
        return R.ok().data(map);
    }
    @PostMapping("addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner) {
        boolean b = bannerService.save(crmBanner);
        return b ? R.ok() : R.error();
    }
    @PostMapping("updateBanner")
    public R updateBanner(@RequestBody CrmBanner crmBanner) {
        boolean b = bannerService.updateById(crmBanner);
        return b ? R.ok() : R.error();
    }
    @DeleteMapping("deleteBannerById/{id}")
    public R deleteBannerById(@PathVariable String id) {
        boolean b = bannerService.removeById(id);
        return b ? R.ok() : R.error();
    }
    @GetMapping("getBannerById/{id}")
    public R getBannerById(@PathVariable String id) {
        CrmBanner banner = bannerService.getById(id);
        return R.ok().data("item",banner);
    }
}

